﻿using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Windows.Controls;
using eLearning.Util;
using Microsoft.Phone.Controls;

namespace eLearning.Controls
{
    public partial class StorageBrowser : PhoneApplicationPage
    {
        static PhoneApplicationPage callerPage;

        IsolatedStorageFile fileStorage;
        string selectedFile;
        string lastDirectory;

        public StorageBrowser()
        {
            InitializeComponent();

            fileStorage = FileUtil.GetUserStorage();
            GetListContents(null);
        }

        public static void CallerPage(PhoneApplicationPage page)
        {
            callerPage = page;
        }

        private void GetListContents(string folder)
        {
            List<DirectorySystem> listDirectorySystem = new List<DirectorySystem>();
            List<DirectorySystem> listFileSystem = new List<DirectorySystem>();

            string[] directories = folder == null ? fileStorage.GetDirectoryNames() : fileStorage.GetDirectoryNames(fileStorage.DirectoryExists(folder) ? folder + "\\*" : folder);
            string[] directoryFiles = folder == null ? fileStorage.GetFileNames() : fileStorage.GetFileNames(fileStorage.DirectoryExists(folder) ? folder + "\\*" : folder);

            foreach (string dir in directories)
            {
                DirectorySystem newDir = new DirectorySystem();
                newDir.DirectoryName = dir;
                listDirectorySystem.Add(newDir);
            }

            foreach (string file in directoryFiles)
            {
                DirectorySystem newFile = new DirectorySystem();
                newFile.DirectoryName = folder;
                newFile.FileName = file;
                listFileSystem.Add(newFile);
            }

            this.ContentsFolderListBox.ItemsSource = listDirectorySystem;
            this.ContentsFileListBox.ItemsSource = listFileSystem;
        }

        private void DirectoryListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                DirectorySystem dirs = (DirectorySystem)e.AddedItems[0];
                GetListContents(dirs.DirectoryName);
                lastDirectory = dirs.DirectoryName;

                ((ListBox)sender).SelectedIndex = -1;
            }
        }

        private void ContentsFileListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                DirectorySystem file = (DirectorySystem)e.AddedItems[0];
                selectedFile = file.ToString();

                BackToCallerPage();
            }
        }

        private void BackToCallerPage()
        {
            if (callerPage is eLearning.DosenView.ResourcesFormPage)
            {
                eLearning.DosenView.ResourcesFormPage resources = (eLearning.DosenView.ResourcesFormPage)callerPage;
                if(selectedFile != null)
                    resources.ReadFile(selectedFile, FileUtil.ReadFileStream(selectedFile));
            }
            else if (callerPage is CsvParser)
            {
                CsvParser csv = (CsvParser)callerPage;
                if (selectedFile != null)
                {
                    csv.SelectedFile(selectedFile);
                    csv.ParseCSVFile();
                }
            }
            else if (callerPage is eLearning.MahasiswaView.TugasDetailPage)
            {
                eLearning.MahasiswaView.TugasDetailPage tugas = (eLearning.MahasiswaView.TugasDetailPage)callerPage;
                if (selectedFile != null)
                {
                    if (selectedFile != null)
                        tugas.ReadFile(selectedFile, FileUtil.ReadFileStream(selectedFile));
                }
            }
            NavigationService.GoBack();
        }

        private void buttonUp_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            GetListContents(lastDirectory);
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            base.OnBackKeyPress(e);
        }

        private void backButton_Click(object sender, System.EventArgs e)
        {
            BackToCallerPage();
        }

    }

    public class DirectorySystem
    {
        public string DirectoryName { get; set; }
        public string FileName { get; set; }
        public string[] DirectoryFiles { get; set; }

        public override string ToString()
        {
            return DirectoryName + "/" + FileName;
        }
    }
}